package io.modifier.basic.compose.widget.comm

import android.content.res.Configuration
import androidx.compose.foundation.background
import androidx.compose.foundation.border
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.text.BasicTextField
import androidx.compose.foundation.text.KeyboardOptions
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.graphics.SolidColor
import androidx.compose.ui.text.style.TextAlign
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.Dp
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import io.modifier.basic.compose.theme.AppTheme
import io.modifier.basic.compose.theme.AppThemeHolder
import io.modifier.basic.emo.core.runIf

@Composable
fun BasicInputBox(
    value: String,
    onValueChange: (String) -> Unit,
    modifier: Modifier = Modifier,
    hint: String = "",
    singleLine: Boolean = true,
    horizontalPadding: Dp = AppThemeHolder.config.gap,
    cursorColor: Color = AppThemeHolder.colors.appColor,
    keyboardOptions: KeyboardOptions = KeyboardOptions.Default,
) {
    BasicTextField(
        modifier = modifier,
        value = value,
        onValueChange = onValueChange,
        keyboardOptions = keyboardOptions,
        singleLine = singleLine,
        textStyle = AppThemeHolder.textStyle.content,
        cursorBrush = SolidColor(cursorColor),
        decorationBox = { innerTextField ->
            Row(
                modifier = Modifier.padding(horizontal = horizontalPadding),
                verticalAlignment = Alignment.CenterVertically
            ) {
                Box(contentAlignment = Alignment.CenterStart) {
                    if (value.isEmpty()) {
                        Text(
                            text = hint,
                            color = AppThemeHolder.colors.txtGray,
                            fontSize = 14.sp,
                            textAlign = TextAlign.Center
                        )
                    }
                    innerTextField()
                }
            }
        }
    )
}

@Composable
fun InputWithBorder(
    value: String,
    onValueChange: (String) -> Unit,
    modifier: Modifier = Modifier,
    heightDp: Int = 50,
    horizontalPadding: Dp = AppThemeHolder.config.gap,
    hint: String = "",
    singleLine: Boolean = true,
    borderColor: Color = AppThemeHolder.colors.dividerColor,
    keyboardOptions: KeyboardOptions = KeyboardOptions.Default,
) {
    Box(
        modifier = modifier
            .runIf(heightDp > 0, { height(heightDp.dp) })
            .runIf(horizontalPadding > 0.dp, { padding(horizontal = horizontalPadding) })
            .border(1.dp, borderColor, AppThemeHolder.config.round)
            .padding(horizontal = AppThemeHolder.config.smallGap)
    ) {
        BasicInputBox(
            value,
            onValueChange,
            modifier = Modifier.fillMaxSize(),
            hint = hint,
            singleLine = singleLine,
            horizontalPadding = 0.dp,
            keyboardOptions = keyboardOptions
        )
    }
}

@Composable
private fun PreviewInput() {
    AppTheme {
        Column(Modifier.background(Color.White)) {

            BasicInputBox(
                "",
                onValueChange = {

                },
                modifier = Modifier
                    .fillMaxWidth()
                    .height(50.dp),
                hint = "请输入",
            )

            V(15)
            InputWithBorder(
                "",
                onValueChange = {
                },
                hint = "请输入",
            )

        }
    }
}

@Preview(backgroundColor = 0xFF121212, showBackground = true, uiMode = Configuration.UI_MODE_NIGHT_NO)
@Composable
private fun PreviewLightInput() {
    PreviewInput()
}

//@Preview(backgroundColor = 0xFFf5f5f5, showBackground = true, uiMode = Configuration.UI_MODE_NIGHT_YES)
@Composable
private fun PreviewDarkInput() {
    PreviewInput()
}